package com.example.dell.fastshopping.mall;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.mall.entity.HeadTab;
import com.example.dell.fastshopping.mall.entity.MallEntity;
import com.example.dell.fastshopping.mall.model.DuoBaoMallViewModel;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.utils.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ${YSH} on 2019/4/11.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */

public class DuoBaoMallActivity extends FragmentActivity {

    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView titlehead;

    private DuoBaoMallViewModel viewModel;
    private GridLayoutManager manager;
    private List<MallEntity> data;
    private List<String> title;
    ////0: 全部 1: 进行中 2: 即将开始  3: 已结束
    private List<DuoBaoMallFragment> fragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duobao_mall);
        ButterKnife.bind(this);
        viewModel = ViewModelProviders.of(this).get(DuoBaoMallViewModel.class);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        final int id = intent.getIntExtra("id", -1);
        titlehead.setText("竞拍列表");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tab.setupWithViewPager(vp);
        tab.setTabIndicatorFullWidth(false);
        vp.setOffscreenPageLimit(5);

        fragments = new ArrayList<>();
        title = new ArrayList<>();
        viewModel.getHeadTable().observe(this, new Observer<List<HeadTab>>() {
            @Override
            public void onChanged(@Nullable List<HeadTab> lists) {
                for (HeadTab headTab : lists) {
                    title.add(headTab.getLabel());
                    fragments.add(DuoBaoMallFragment.newInstant(headTab.getValue(), id));
                    setMyAdapter();
                }
            }
        });
    }

    private void setMyAdapter() {
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title.get(position);
            }
        });
    }
}
